# Author: Andrea Junqueira
# Project: Janusz, Cunha, and Junqueira
# Goal: SI for Women Analysis
# First Version: Jun 22, 2023
# This Version: Jun 11th, 2024

rm(list = ls())

# Libraries
library(rdrobust)
library(here)
library(tidyverse)
library(sandwich)
library(lmtest)
library(MASS)
library(stargazer)
library(texreg)

# Read genFunctions
source(here("R-Files", "genFunctions.R"))

# Load data
load(file = here("Data", "womenRDD_final.RData"))


# Session ran on 06/11/2024
# > sessionInfo()
# R version 4.1.2 (2021-11-01)
# Platform: x86_64-pc-linux-gnu (64-bit)
# Running under: Linux Mint 21.1
# 
# Matrix products: default
# BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0
# LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
# 
# locale:
#   [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
# [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
# [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
# 
# attached base packages:
#   [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
#   [1] stargazer_5.2.3 MASS_7.3-55     lmtest_0.9-40   zoo_1.8-12      sandwich_3.0-2  lubridate_1.9.2 forcats_1.0.0  
# [8] stringr_1.5.0   dplyr_1.1.2     purrr_1.0.1     readr_2.1.4     tidyr_1.3.0     tibble_3.2.1    ggplot2_3.4.2  
# [15] tidyverse_2.0.0 here_1.0.1      rdrobust_2.1.1 
# 
# loaded via a namespace (and not attached):
#   [1] pillar_1.9.0     compiler_4.1.2   tools_4.1.2      lattice_0.20-45  lifecycle_1.0.3  gtable_0.3.3     timechange_0.2.0
# [8] pkgconfig_2.0.3  rlang_1.1.1      cli_3.6.1        rstudioapi_0.14  withr_2.5.0      generics_0.1.3   vctrs_0.6.3     
# [15] hms_1.1.3        rprojroot_2.0.3  grid_4.1.2       tidyselect_1.2.0 glue_1.6.2       R6_2.5.1         fansi_1.0.4     
# [22] tzdb_0.4.0       magrittr_2.0.3   scales_1.2.1     colorspace_2.1-0 utf8_1.2.3       stringi_1.7.12   munsell_0.5.0

#######################################################
########### Part 1 - Descriptive Statistics ###########
#######################################################

# variables to summary stats
vars <- c('run_var_woman', 'totalWomen', 'totalWomen_ratio', 'totalWomen_pct',
          'totalWomen_noexp', 'totalWomen_noexp_ratio', 'totalWomen_noexp_pct',
          'totalWomen_noexp_frommp_pct', 'totalWomen_noexp_frommp_ratio', 'totalWomen_noexp_frommp_pct')

stargazer(womenRDD[, vars],
          covariate.labels = c('Running Variable', 
                               '# of Women Candidates',
                               'Ratio of Women Candidates Per Seat',
                               '% of Women Candidates Per Seat',
                               '# of Women Candidates (No Political Experience)',
                               'Ratio of Women Candidates Per Seat (No Political Experience)',
                               '% of Women Candidates Per Seat (No Political Experience)',                               
                               '# of Women Candidates (No Political Experience from Mayoral Party)',
                               'Ratio of Women Candidates Per Seat (No Political Experience from Mayoral Party)',
                               '% of Women Candidates Per Seat (No Political Experience from Mayoral Party)'                               
          ),
          title = 'Descriptive Statistics for the Women Candidate Emergence Analysis'
)

##############################################################
########### Part 2 - Models With Control Variables ###########
##############################################################

# control variables
control.vars <- c('black', 'times.as.candidate', 'age', 'times.as.winner',
                  'incumbent', 'married', 'college', 'merchant',
                  'politician', 'servant', 'business', 'doctor',
                  'administrator', 'farmer',
                  'black_pop_pct', 'urban_pct', 'ln_gdp_pc', 'female_pct', 'population')

############### No Restrictions

# Women Candidates
m1a <- rdrobust(y = womenRDD$totalWomen,
                x = womenRDD$run_var_woman,
                covs = womenRDD[, control.vars],
                all = TRUE)
summary(m1a) # Negative, null


# Women Candidates (Ratio)
m1b <- rdrobust(y = womenRDD$totalWomen_ratio,
                x = womenRDD$run_var_woman,
                covs = womenRDD[, control.vars],                
                all = TRUE)
summary(m1b) # Negative, Null


# Women Candidates (pct)
m1c <- rdrobust(y = womenRDD$totalWomen_pct,
                x = womenRDD$run_var_woman,
                covs = womenRDD[, control.vars],                  
                all = TRUE)
summary(m1c) # Positive, Null


############### No Experience

# Women Candidates
m2a  <- rdrobust(y = womenRDD$totalWomen_noexp,
                 x = womenRDD$run_var_woman,
                 covs = womenRDD[, control.vars],                  
                 all = TRUE)
summary(m2a) # Negative, Null

# Women Candidates (ratio)
m2b  <- rdrobust(y = womenRDD$totalWomen_noexp_ratio,
                 x = womenRDD$run_var_woman,
                 covs = womenRDD[, control.vars],                  
                 all = TRUE)
summary(m2b) # Negative, Null

# Women Candidates (pct)
m2c  <- rdrobust(y = womenRDD$totalWomen_noexp_pct,
                 x = womenRDD$run_var_woman,
                 covs = womenRDD[, control.vars],                  
                 all = TRUE)
summary(m2c) # Positive, Null

############### No Experience From Mayoral Party

# Women Candidates
m3a  <- rdrobust(y = womenRDD$totalWomen_noexp_frommp,
                 x = womenRDD$run_var_woman,
                 covs = womenRDD[, control.vars],                  
                 all = TRUE)
summary(m3a) # Positive, Null

# Women Candidates (ratio)
m3b  <- rdrobust(y = womenRDD$totalWomen_noexp_frommp_ratio,
                 x = womenRDD$run_var_woman,
                 covs = womenRDD[, control.vars],                  
                 all = TRUE)
summary(m3b) # Positive, Null

# Women Candidates (pct)
m3c  <- rdrobust(y = womenRDD$totalWomen_noexp_frommp_pct,
                 x = womenRDD$run_var_woman,
                 covs = womenRDD[, control.vars],                  
                 all = TRUE)
summary(m3c) # Positive, Null

#####################################################
########### Part 3 - Sensitivity Analysis ###########
#####################################################

############### No Restrictions
pdf(here('plots_tables', 'Sens.WomenCandidates.pdf'), height = 8, width = 8)
sensitivity(mydata = womenRDD, 
            DV = 'totalWomen', 
            run_var = 'run_var_woman',
            ylabel = '# of Women Candidates',
            ylb = -50, yub = 50)
sensitivity(mydata = womenRDD, 
            DV = 'totalWomen_ratio', 
            run_var = 'run_var_woman',
            ylabel = 'Ratio of Women Candidates Per Seat',
            ylb = -5, yub = 5)
sensitivity(mydata = womenRDD, 
            DV = 'totalWomen_pct', 
            run_var = 'run_var_woman',
            ylabel = '% of Women Candidates',
            ylb = -10, yub = 10)
dev.off()


############### No Experience
pdf(here('plots_tables', 'Sens.WomenCandidates_NoExp.pdf'), height = 8, width = 8)
sensitivity(mydata = womenRDD, 
            DV = 'totalWomen_noexp', 
            run_var = 'run_var_woman',
            ylabel = '# of Women Candidates',
            ylb = -40, yub = 40)
sensitivity(mydata = womenRDD, 
            DV = 'totalWomen_noexp_ratio', 
            run_var = 'run_var_woman',
            ylabel = 'Ratio of Women Candidates Per Seat',
            ylb = -5, yub = 5)
sensitivity(mydata = womenRDD, 
            DV = 'totalWomen_noexp_pct', 
            run_var = 'run_var_woman',
            ylabel = '% of Women Candidates',
            ylb = -10, yub = 10)
dev.off()

############### No Experience From Mayoral Party
pdf(here('plots_tables', 'Sens.WomenCandidates_NoExpMayoral.pdf'), height = 8, width = 8)
sensitivity(mydata = womenRDD, 
            DV = 'totalWomen_noexp_frommp', 
            run_var = 'run_var_woman',
            ylabel = '# of Women Candidates',
            ylb = -5, yub = 5)
sensitivity(mydata = womenRDD, 
            DV = 'totalWomen_noexp_frommp_ratio', 
            run_var = 'run_var_woman',
            ylabel = 'Ratio of Women Candidates Per Seat',
            ylb = -0.5, yub = 0.5)
sensitivity(mydata = womenRDD, 
            DV = 'totalWomen_noexp_frommp_pct', 
            run_var = 'run_var_woman',
            ylabel = '% of Women Candidates',
            ylb = -60, yub = 60)
dev.off()


#################################################################
########### Part 4 - Models For Different Polynomials ###########
#################################################################

# Polynomial value
polynomial <- 4

############### No Restrictions

# Women Candidates
m1a <- rdrobust(y = womenRDD$totalWomen,
                x = womenRDD$run_var_woman,
                p = polynomial,
                all = TRUE)
summary(m1a) # Negative, p<.033


# Women Candidates (Ratio)
m1b <- rdrobust(y = womenRDD$totalWomen_ratio,
                x = womenRDD$run_var_woman,
                p = polynomial,                
                all = TRUE)
summary(m1b) # Negative, p==.05


# Women Candidates (pct)
m1c <- rdrobust(y = womenRDD$totalWomen_pct,
                x = womenRDD$run_var_woman,
                p = polynomial,                
                all = TRUE)
summary(m1c) # Positive, Null


############### No Experience

# Women Candidates
m2a  <- rdrobust(y = womenRDD$totalWomen_noexp,
                 x = womenRDD$run_var_woman,
                 p = polynomial,                 
                 all = TRUE)
summary(m2a) # Negative, p<.05

# Women Candidates (ratio)
m2b  <- rdrobust(y = womenRDD$totalWomen_noexp_ratio,
                 x = womenRDD$run_var_woman,
                 p = polynomial,                 
                 all = TRUE)
summary(m2b) # Negative, p<.05

# Women Candidates (pct)
m2c  <- rdrobust(y = womenRDD$totalWomen_noexp_pct,
                 x = womenRDD$run_var_woman,
                 p = polynomial,                 
                 all = TRUE)
summary(m2c) # Negative, Null

############### No Experience From Mayoral Party

# Women Candidates
m3a  <- rdrobust(y = womenRDD$totalWomen_noexp_frommp,
                 x = womenRDD$run_var_woman,
                 p = polynomial,               
                 all = TRUE)
summary(m3a) # Negative, Null

# Women Candidates (ratio)
m3b  <- rdrobust(y = womenRDD$totalWomen_noexp_frommp_ratio,
                 x = womenRDD$run_var_woman,
                 p = polynomial,                 
                 all = TRUE)
summary(m3b) # Negative, Null

# Women Candidates (pct)
m3c  <- rdrobust(y = womenRDD$totalWomen_noexp_frommp_pct,
                 x = womenRDD$run_var_woman,
                 p = polynomial,                
                 all = TRUE)
summary(m3c) # Positive, Null

###############################################
########### Part 5 - Power Analysis ###########
###############################################

############### No Restrictions
p1a <- rdsampsi(data = na.omit(womenRDD[, c('totalWomen', 'run_var_woman')]), 
                tau = sd(womenRDD$totalWomen, na.rm = TRUE)/2, cutoff = 0)
p1b <- rdsampsi(data = na.omit(womenRDD[, c('totalWomen_ratio', 'run_var_woman')]), 
                tau = sd(womenRDD$totalWomen_ratio, na.rm = TRUE)/2, cutoff = 0)
p1c <- rdsampsi(data = na.omit(womenRDD[, c('totalWomen_pct', 'run_var_woman')]), 
                tau = sd(womenRDD$totalWomen_pct, na.rm = TRUE)/2, cutoff = 0)

############### No Experience
p2a <- rdsampsi(data = na.omit(womenRDD[, c('totalWomen_noexp', 'run_var_woman')]), 
                tau = sd(womenRDD$totalWomen_noexp, na.rm = TRUE)/2, cutoff = 0)
p2b <- rdsampsi(data = na.omit(womenRDD[, c('totalWomen_noexp_ratio', 'run_var_woman')]), 
                tau = sd(womenRDD$totalWomen_noexp_ratio, na.rm = TRUE)/2, cutoff = 0)
p2c <- rdsampsi(data = na.omit(womenRDD[, c('totalWomen_noexp_pct', 'run_var_woman')]), 
                tau = sd(womenRDD$totalWomen_noexp_pct, na.rm = TRUE)/2, cutoff = 0)

############### No Experience From Mayoral Party
p3a <- rdsampsi(data = na.omit(womenRDD[, c('totalWomen_noexp_frommp', 'run_var_woman')]), 
                tau = sd(womenRDD$totalWomen_noexp_frommp, na.rm = TRUE)/2, cutoff = 0)
p3b <- rdsampsi(data = na.omit(womenRDD[, c('totalWomen_noexp_frommp_ratio', 'run_var_woman')]), 
                tau = sd(womenRDD$totalWomen_noexp_frommp_ratio, na.rm = TRUE)/2, cutoff = 0)
p3c <- rdsampsi(data = na.omit(womenRDD[, c('totalWomen_noexp_frommp_pct', 'run_var_woman')]), 
                tau = sd(womenRDD$totalWomen_noexp_frommp_pct, na.rm = TRUE)/2, cutoff = 0)

#################################################
########### Part 6 - Assumption Tests ###########
#################################################

#-------- Density Test ------#
# Directory to save plots
summary(t1 <- rddensity(womenRDD$run_var_woman, c = 0)) # No manipulation
# Result: No sorting
# Density Plot 
pdf(here('plots_tables', 'Women_density.pdf'), height = 8, width = 12)
par(mar = c(6, 6, 2, 2))
hist(womenRDD$run_var_woman, breaks = 75, # at the bw
     main = "", xlab = "Difference in Vote Share", 
     xlim = c(-50, 50),
     ylim = c(0, 200),
     cex.lab = 1.75, 
     cex.axis = 1.5,
     col = 'grey90')
abline(v = 0, col = "black", lwd = 8)
text(x = 30, y = 75, labels = paste("p-value = ", round(t1$test$p_jk, 3)), cex = 1.75)
dev.off()

# Balance Tests
summary(rdrobust(y = womenRDD$population,
                 x = womenRDD$run_var_woman,
                 all = TRUE))
summary(rdrobust(y = womenRDD$black,
                 x = womenRDD$run_var_woman,
                 all = TRUE))
summary(rdrobust(y = womenRDD$college,
                 x = womenRDD$run_var_woman,
                 all = TRUE))
summary(rdrobust(y = womenRDD$married,
                 x = womenRDD$run_var_woman,
                 all = TRUE))
summary(rdrobust(y = womenRDD$age,
                 x = womenRDD$run_var_woman,
                 all = TRUE))
summary(rdrobust(y = womenRDD$merchant,
                 x = womenRDD$run_var_woman,
                 all = TRUE))
summary(rdrobust(y = womenRDD$servant,
                 x = womenRDD$run_var_woman,
                 all = TRUE))
summary(rdrobust(y = womenRDD$administrator,
                 x = womenRDD$run_var_woman,
                 all = TRUE))
summary(rdrobust(y = womenRDD$farmer,
                 x = womenRDD$run_var_woman,
                 all = TRUE))
summary(rdrobust(y = womenRDD$business,
                 x = womenRDD$run_var_woman,
                 all = TRUE))
summary(rdrobust(y = womenRDD$doctor,
                 x = womenRDD$run_var_woman,
                 all = TRUE))
summary(rdrobust(y = womenRDD$politician,
                 x = womenRDD$run_var_woman,
                 all = TRUE))
summary(rdrobust(y = womenRDD$incumbent,
                 x = womenRDD$run_var_woman,
                 all = TRUE))
summary(rdrobust(y = womenRDD$times.as.candidate,
                 x = womenRDD$run_var_woman,
                 all = TRUE))
summary(rdrobust(y = womenRDD$times.as.winner,
                 x = womenRDD$run_var_woman,
                 all = TRUE))
summary(rdrobust(y = womenRDD$black_pop_pct,
                 x = womenRDD$run_var_woman,
                 all = TRUE))
summary(rdrobust(y = womenRDD$female_pct,
                 x = womenRDD$run_var_woman,
                 all = TRUE))
summary(rdrobust(y = womenRDD$urban_pct,
                 x = womenRDD$run_var_woman,
                 all = TRUE))
summary(rdrobust(y = womenRDD$ln_gdp_pc,
                 x = womenRDD$run_var_woman,
                 all = TRUE))

################################################
########### Part 7 - Results by Year ###########
################################################

year = 2020

###################
# No Restrictions #
###################

# Women Candidates
m1a <- rdrobust(y = womenRDD$totalWomen[womenRDD$ANO_ELEICAO==year],
                x = womenRDD$run_var_woman[womenRDD$ANO_ELEICAO==year],
                all = TRUE)
summary(m1a) 

# Women Candidates (Ratio)
m1b <- rdrobust(y = womenRDD$totalWomen_ratio[womenRDD$ANO_ELEICAO==year],
                x = womenRDD$run_var_woman[womenRDD$ANO_ELEICAO==year],
                all = TRUE)
summary(m1b) 

# Women Candidates (pct)
m1c <- rdrobust(y = womenRDD$totalWomen_pct[womenRDD$ANO_ELEICAO==year],
                x = womenRDD$run_var_woman[womenRDD$ANO_ELEICAO==year],
                all = TRUE)
summary(m1c) 

#################
# No Experience #
#################

# Women Candidates
m2a  <- rdrobust(y = womenRDD$totalWomen_noexp[womenRDD$ANO_ELEICAO==year],
                 x = womenRDD$run_var_woman[womenRDD$ANO_ELEICAO==year],
                 all = TRUE)
summary(m2a) 

# Women Candidates (ratio)
m2b  <- rdrobust(y = womenRDD$totalWomen_noexp_ratio[womenRDD$ANO_ELEICAO==year],
                 x = womenRDD$run_var_woman[womenRDD$ANO_ELEICAO==year],
                 all = TRUE)
summary(m2b) 

# Women Candidates (pct)
m2c  <- rdrobust(y = womenRDD$totalWomen_noexp_pct[womenRDD$ANO_ELEICAO==year],
                 x = womenRDD$run_var_woman[womenRDD$ANO_ELEICAO==year],
                 all = TRUE)
summary(m2c) 

##############################
# No Experience From Mayoral #
##############################

# Women Candidates
m3a  <- rdrobust(y = womenRDD$totalWomen_noexp_frommp[womenRDD$ANO_ELEICAO==year],
                 x = womenRDD$run_var_woman[womenRDD$ANO_ELEICAO==year],
                 all = TRUE)
summary(m3a) 

# Women Candidates (ratio)
m3b  <- rdrobust(y = womenRDD$totalWomen_noexp_frommp_ratio[womenRDD$ANO_ELEICAO==year],
                 x = womenRDD$run_var_woman[womenRDD$ANO_ELEICAO==year],
                 all = TRUE)
summary(m3b) 

# Women Candidates (pct)
m3c  <- rdrobust(y = womenRDD$totalWomen_noexp_frommp_pct[womenRDD$ANO_ELEICAO==year],
                 x = womenRDD$run_var_woman[womenRDD$ANO_ELEICAO==year],
                 all = TRUE)
summary(m3c) 


###############################################
### Part 8 - Results including 2000-2008 ######
###############################################

load(file = here("Data", "womenRDD_since2000_final.RData"))

# Part 1 - No Restrictions #

# Women Candidates
m1a <- rdrobust(y = womenRDD_since2000$totalWomen,
                x = womenRDD_since2000$run_var_woman,
                all = TRUE)
summary(m1a) # Negative, p <.05

# Women Candidates (Ratio)
m1b <- rdrobust(y = womenRDD_since2000$totalWomen_ratio,
                x = womenRDD_since2000$run_var_woman,
                all = TRUE)
summary(m1b) # Negative, Null

# Women Candidates (pct)
m1c <- rdrobust(y = womenRDD_since2000$totalWomen_pct,
                x = womenRDD_since2000$run_var_woman,
                all = TRUE)
summary(m1c) # Positive, Null


# Part 2 - No Experience #

# Women Candidates
m2a  <- rdrobust(y = womenRDD_since2000$totalWomen_noexp,
                 x = womenRDD_since2000$run_var_woman,
                 all = TRUE)
summary(m2a) # Negative, p <.05

# Women Candidates (ratio)
m2b  <- rdrobust(y = womenRDD_since2000$totalWomen_noexp_ratio,
                 x = womenRDD_since2000$run_var_woman,
                 all = TRUE)
summary(m2b) # Negative, Null

# Women Candidates (pct)
m2c  <- rdrobust(y = womenRDD_since2000$totalWomen_noexp_pct,
                 x = womenRDD_since2000$run_var_woman,
                 all = TRUE)
summary(m2c) # Positive, Null


# Part 3 - No Experience From Mayoral #

# Women Candidates
m3a  <- rdrobust(y = womenRDD_since2000$totalWomen_noexp_frommp,
                 x = womenRDD_since2000$run_var_woman,
                 all = TRUE)
summary(m3a) # Positive, Null

# Women Candidates (ratio)
m3b  <- rdrobust(y = womenRDD_since2000$totalWomen_noexp_frommp_ratio,
                 x = womenRDD_since2000$run_var_woman,
                 all = TRUE)
summary(m3b) # Positive, Null

# Women Candidates (pct)
m3c  <- rdrobust(y = womenRDD_since2000$totalWomen_noexp_frommp_pct,
                 x = womenRDD_since2000$run_var_woman,
                 all = TRUE)
summary(m3c) # Positive, Null

#-----

## Results considering only the period prior to the quotas

# Part 1 - No Restrictions #

womenRDD_since2000_sub = womenRDD_since2000 %>%
  dplyr::filter(ANO_ELEICAO %in% c(2000,2004,2008))

# Women Candidates
m1a <- rdrobust(y = womenRDD_since2000_sub$totalWomen,
                x = womenRDD_since2000_sub$run_var_woman,
                all = TRUE)
summary(m1a) # Negative, p <.05

# Women Candidates (Ratio)
m1b <- rdrobust(y = womenRDD_since2000_sub$totalWomen_ratio,
                x = womenRDD_since2000_sub$run_var_woman,
                all = TRUE)
summary(m1b) # Negative, Null

# Women Candidates (pct)
m1c <- rdrobust(y = womenRDD_since2000_sub$totalWomen_pct,
                x = womenRDD_since2000_sub$run_var_woman,
                all = TRUE)
summary(m1c) # Positive, Null


# Part 2 - No Experience #

# Women Candidates
m2a  <- rdrobust(y = womenRDD_since2000_sub$totalWomen_noexp,
                 x = womenRDD_since2000_sub$run_var_woman,
                 all = TRUE)
summary(m2a) # Negative, p <.05

# Women Candidates (ratio)
m2b  <- rdrobust(y = womenRDD_since2000_sub$totalWomen_noexp_ratio,
                 x = womenRDD_since2000_sub$run_var_woman,
                 all = TRUE)
summary(m2b) # Negative, Null

# Women Candidates (pct)
m2c  <- rdrobust(y = womenRDD_since2000_sub$totalWomen_noexp_pct,
                 x = womenRDD_since2000_sub$run_var_woman,
                 all = TRUE)
summary(m2c) # Positive, Null


# Part 3 - No Experience From Mayoral #

# Women Candidates
m3a  <- rdrobust(y = womenRDD_since2000_sub$totalWomen_noexp_frommp,
                 x = womenRDD_since2000_sub$run_var_woman,
                 all = TRUE)
summary(m3a) # Positive, Null

# Women Candidates (ratio)
m3b  <- rdrobust(y = womenRDD_since2000_sub$totalWomen_noexp_frommp_ratio,
                 x = womenRDD_since2000_sub$run_var_woman,
                 all = TRUE)
summary(m3b) # Positive, Null

# Women Candidates (pct)
m3c  <- rdrobust(y = womenRDD_since2000_sub$totalWomen_noexp_frommp_pct,
                 x = womenRDD_since2000_sub$run_var_woman,
                 all = TRUE)
summary(m3c) # Positive, Null

#-----


## Results by Year ##

year = 2008

# No Restrictions #

# Women Candidates
m1a <- rdrobust(y = womenRDD_since2000$totalWomen[womenRDD_since2000$ANO_ELEICAO==year],
                x = womenRDD_since2000$run_var_woman[womenRDD_since2000$ANO_ELEICAO==year],
                all = TRUE)
summary(m1a) 

# Women Candidates (Ratio)
m1b <- rdrobust(y = womenRDD_since2000$totalWomen_ratio[womenRDD_since2000$ANO_ELEICAO==year],
                x = womenRDD_since2000$run_var_woman[womenRDD_since2000$ANO_ELEICAO==year],
                all = TRUE)
summary(m1b) 

# Women Candidates (pct)
m1c <- rdrobust(y = womenRDD_since2000$totalWomen_pct[womenRDD_since2000$ANO_ELEICAO==year],
                x = womenRDD_since2000$run_var_woman[womenRDD_since2000$ANO_ELEICAO==year],
                all = TRUE)
summary(m1c) 


# No Experience #

# Women Candidates
m2a  <- rdrobust(y = womenRDD_since2000$totalWomen_noexp[womenRDD_since2000$ANO_ELEICAO==year],
                 x = womenRDD_since2000$run_var_woman[womenRDD_since2000$ANO_ELEICAO==year],
                 all = TRUE)
summary(m2a) 

# Women Candidates (ratio)
m2b  <- rdrobust(y = womenRDD_since2000$totalWomen_noexp_ratio[womenRDD_since2000$ANO_ELEICAO==year],
                 x = womenRDD_since2000$run_var_woman[womenRDD_since2000$ANO_ELEICAO==year],
                 all = TRUE)
summary(m2b) 

# Women Candidates (pct)
m2c  <- rdrobust(y = womenRDD_since2000$totalWomen_noexp_pct[womenRDD_since2000$ANO_ELEICAO==year],
                 x = womenRDD_since2000$run_var_woman[womenRDD_since2000$ANO_ELEICAO==year],
                 all = TRUE)
summary(m2c) 


# No Experience From Mayoral #

# Women Candidates
m3a  <- rdrobust(y = womenRDD_since2000$totalWomen_noexp_frommp[womenRDD_since2000$ANO_ELEICAO==year],
                 x = womenRDD_since2000$run_var_woman[womenRDD_since2000$ANO_ELEICAO==year],
                 all = TRUE)
summary(m3a) 

# Women Candidates (ratio)
m3b  <- rdrobust(y = womenRDD_since2000$totalWomen_noexp_frommp_ratio[womenRDD_since2000$ANO_ELEICAO==year],
                 x = womenRDD_since2000$run_var_woman[womenRDD_since2000$ANO_ELEICAO==year],
                 all = TRUE)
summary(m3b) 

# Women Candidates (pct)
m3c  <- rdrobust(y = womenRDD_since2000$totalWomen_noexp_frommp_pct[womenRDD_since2000$ANO_ELEICAO==year],
                 x = womenRDD_since2000$run_var_woman[womenRDD_since2000$ANO_ELEICAO==year],
                 all = TRUE)
summary(m3c) 


################################################################################
### Part 10 - Candidates with Experience V. Candidates Without Experience ######
################################################################################

#### No Experience
# Women Candidates
mNo1  <- rdrobust(y = womenRDD$totalWomen_noexp,
                  x = womenRDD$run_var_woman,
                  all = TRUE)
summary(mNo1) # Negative, p<0.05

# Women Candidates (ratio)
mNo2  <- rdrobust(y = womenRDD$totalWomen_noexp_ratio,
                  x = womenRDD$run_var_woman,
                  all = TRUE)
summary(mNo2) # Negative, Null

# Women Candidates (pct)
mNo3  <- rdrobust(y = womenRDD$totalWomen_noexp_pct,
                  x = womenRDD$run_var_woman,
                  all = TRUE)
summary(mNo3) # Negative, Null

#### With Experience
# Women Candidates
mW1  <- rdrobust(y = womenRDD$totalWomen_exp,
                 x = womenRDD$run_var_woman,
                 all = TRUE)
summary(mW1) # Negative, Null

# Women Candidates (ratio)
mW2  <- rdrobust(y = womenRDD$totalWomen_exp_ratio,
                 x = womenRDD$run_var_woman,
                 all = TRUE)
summary(mW2) # Negative, Null

# Women Candidates (pct)
mW3  <- rdrobust(y = womenRDD$totalWomen_exp_pct,
                 x = womenRDD$run_var_woman,
                 all = TRUE)
summary(mW3) # Positive, Null


# Difference in Coefficients
zDif(beta1 = mNo1$Estimate[1, 2], se1 = mNo1$Estimate[1, 4], beta2 = mW1$Estimate[1, 2], se2 = mW1$Estimate[1, 4])
zDif(beta1 = mNo2$Estimate[1, 2], se1 = mNo2$Estimate[1, 4], beta2 = mW2$Estimate[1, 2], se2 = mW2$Estimate[1, 4])
zDif(beta1 = mNo3$Estimate[1, 2], se1 = mNo3$Estimate[1, 4], beta2 = mW3$Estimate[1, 2], se2 = mW3$Estimate[1, 4])


##################################
### Part 9 - Spatial Models ######
##################################

# Load datasets
load(here("Data","df_spatial_nb_std_final.RData"))

# Immediate neighbors #
# All women candidates #

spec = formula(n_women_cands_council ~ spatial_woman_mayor +
          lag_woman_elected_mayor+
          lag_n_women_elected_council + 
          lag_n_women_cands_council +
          lag_perc_votes_women_council +
          n_vagas_council +
          n_cands_council +
          lag_perc_votes_left_council +
          perc_literate +
          perc_female +
          perc_urban +
          perc_white +
          log_pop +
          pib_pc +
          as.factor(ANO_ELEICAO) +
          as.factor(uf))

m_poisson_women_all = glm(spec, family="poisson", data=df_spatial_nb_std)
m_poisson_women_all_cl = coeftest(m_poisson_women_all, vcov = vcovCL, cluster = ~uf) # standard errors clustered by state


m_neg_bin_women_all = glm.nb(spec, data=df_spatial_nb_std)
m_neg_bin_women_all_cl = coeftest(m_neg_bin_women_all, vcov = vcovCL, cluster = ~uf) # standard errors clustered by state

spec_ols = formula(log_n_women_cands_council ~ spatial_woman_mayor +
                     lag_woman_elected_mayor+
                     lag_n_women_elected_council + 
                     lag_n_women_cands_council +
                     lag_perc_votes_women_council +
                     n_vagas_council +
                     n_cands_council +
                     lag_perc_votes_left_council +
                     perc_literate +
                     perc_female +
                     perc_urban +
                     perc_white +
                     log_pop +
                     pib_pc +
                     as.factor(ANO_ELEICAO) +
                     as.factor(uf))

m_ols_women_all = lm(spec_ols, data=df_spatial_nb_std)
m_ols_women_all_cl = coeftest(m_ols_women_all, vcov = vcovCL, cluster = ~uf) # standard errors clustered by state

texreg(list(m_poisson_women_all_cl,m_neg_bin_women_all_cl,m_ols_women_all_cl))

# Immediate neighbors #
# All women with no experience #

spec = formula(n_cands_women_no_exp_council ~ spatial_woman_mayor +
                 lag_woman_elected_mayor+
                 lag_n_women_elected_council + 
                 lag_n_women_cands_council +
                 lag_perc_votes_women_council +
                 n_vagas_council +
                 n_cands_council +
                 lag_perc_votes_left_council +
                 perc_literate +
                 perc_female +
                 perc_urban +
                 perc_white +
                 log_pop +
                 pib_pc +
                 as.factor(ANO_ELEICAO) +
                 as.factor(uf))

m_poisson_women_noexp = glm(spec, family="poisson", data=df_spatial_nb_std)
m_poisson_women_noexp_cl = coeftest(m_poisson_women_noexp, vcov = vcovCL, cluster = ~uf) # standard errors clustered by state

m_neg_bin_women_noexp = glm.nb(spec, data=df_spatial_nb_std)
m_neg_bin_women_noexp_cl = coeftest(m_neg_bin_women_noexp, vcov = vcovCL, cluster = ~uf) # standard errors clustered by state

spec_ols = formula(log_n_cands_women_no_exp_council ~ spatial_woman_mayor +
                     lag_woman_elected_mayor+
                     lag_n_women_elected_council + 
                     lag_n_women_cands_council +
                     lag_perc_votes_women_council +
                     n_vagas_council +
                     n_cands_council +
                     lag_perc_votes_left_council +
                     perc_literate +
                     perc_female +
                     perc_urban +
                     perc_white +
                     log_pop +
                     pib_pc +
                     as.factor(ANO_ELEICAO) +
                     as.factor(uf))

m_ols_women_noexp = lm(spec_ols, data=df_spatial_nb_std)
m_ols_women_noexp_cl = coeftest(m_ols_women_noexp, vcov = vcovCL, cluster = ~uf)

texreg(list(m_poisson_women_noexp_cl,m_neg_bin_women_noexp_cl,m_ols_women_noexp_cl))



